Pi WALL-E

A cute Self-Balanced Robot

Yucheng Peng (yp284) | Yuchen Lu (yl2237)


Generic placeholder image

This is how Pi WALL-E looks like


Demonstration Video

INTRODUTION

Hi! Before introduction, let us introduce our little boy first! This is our Pi WALL-E robot! This robot is a self-balanced car, and it only has two wheels! Its eye consists of a ginbal with a camera and an Android app connected to our phone. We could see the world through WALL-E’s eye!

MOTIVATION AND INTRODUCTION

In 5725 lab3, our team finish a four-wheel car and control the car to move forward and backward and, we change the speed of the car to move faster and slower. Then an intriguing notion that comes to our mind is to make a two-wheel car to balance itself! Thus, in this final project, we choose to implement our self-balanced car ‘WALL-E’.

The reason why the robot gets its name ‘WALL-E’ is that we set a gimbal with a camera on the top of this car. Its looks just like the ‘WALL-E’ robot. We set an Android app connected to the phone to control the gimbal and rotate the camera, thus we can see the world in different directions from WALL-E’s eye. The Robot WALL-E’s method of balancing and moving stably by reading the moving acceleration to compute corresponding angles. The change of angle will decide the motor speed like when the robot is moving forward, and the angle change will increase the forward speed to balance WALL-E. More details, principles, and results of WALL-E to control its self-balance and see all directions of the world will be illustrated in the following sections. Now, let's go!

FIRST YOU NEED TO KNOW

Components Need:

The balanced robot is a really complicated system and you need much time and patience! Good Luck

By the way, feel Free to contact us! Our email are yp284@cornell.edu and yl2237@cornell.edu.

HOW TO START

We will display the code step by step to show how it works.

You need to connect the Raspberry Pi to all the components like the picture shown below.

graph

Raspberry Pi code

This is our Self-Balanced Robot core control program. We write two version of control program in C and Python.

The C code works successfully to balanced robot so I will follow the C code to tell you how to start.

We didn't success with the python code but we also put our python code on the GitHub. Maybe you can improve it!

C code

Step 1: Install bcm2835 and libi2c-dev

We provide a bcm2835 library in the Library folder and you can also download it from official website

libi2c-dev is default installed on your Linux kernel.

Step2: Load encoder module

We suppose you are familiar with Linux module programing.

With the kernel updated, the header will also changed. You can check your headers in the /usr/src/linux-headers-x.xx.xx and then modify the Makefile file to your kernel version.

Once you set, use the command to compile the encoder module

After compiling, we need to load our module by the following command.

And we can use the command to make sure the encoder module is successfully loaded

lsmod encoder

Step3: Compile C Program

Now, all the pre-requirement are set! Now we can run the control program.

Step4: Start Pi camera

You can see the link below to know how to start pi camera.

Step5: Start RPi as TCP server
Step5: Start Android APP

Just click run button in Android Studio.

Pi WALL-E DETAILS AND THEORIES

WALL-E's Eye

The WALL-E's eye is composed by Gimbal, Pi camera and android phone.

Pi camera

Pi Camrea

The Camera Module 2 can be used to take high-definition video, as well as stills photographs.

Android APP

This Android can simply seen as two parts.

One part is read video data from the Pi Camera.

Android app start a thread and then set up a socket connection with RPi.

Like the following command, after connected, socket will send the string to RPi server.

WALL-E's Body

WALL-E's body is composed by MPU6050, TB6612, 12V Motors with encoders, and breadboard. Here is the code structure of WALL-E's body

structure

TB6612

TB6612 will set as logical high and logical low and control the direction of left motor. As shown in the table:

GPIOOutputControl
5, 6HIGH, LOWLeft Motor Clockwise
5, 6LOW, HIGHLeft Motor Counter-Clockwise
13, 26HIGH, LOWRight Motor Clockwise
13, 26LOW, HIGHRight Motor Counter-Clockwise

Different input to TB6612 can control different direction of motors.

MPU6050

In this section, the key sensor MPU6050 will be illustrated Fig. 1. To keep our two-wheel balanced car ‘WALL-E’ upright and moving forward and backward smoothly, the first thing that needs to obtain is the attitude angle of the car and the change of angle will decide the motor speed. Sensor MPU6050 is a three-direction sensor used to read all acceleration of x, y, and z directions to compute the corresponding moving angle of x, y, and z directions. This computing method will be explained later. Through the angle, the attitude of the car relative to the ground will be obtained to make corresponding control adjustments. For example, when the robot is moving forward, the angle will be computed, and the forward speed will be increased to balance the car.

MPU6050

The angle computing method in this section is the Quaternion method [1]. DMP library is provided in MPU6050. The quaternion method is used in the underlying algorithm and the driver is configured and transplanted to use quaternion to moving angles. Besides, commonly used attitude computation methods also include the Kalman filter and complementary filter methods [2]. In the configure and transplant work of MPU6050, the following work is illustrated in Fig. 2 in inv_mpu.c file

These functions of i2c_write, i2c_read, delay_ms, get_ms, reg_int_cb, labs, fabsf, and min functions need to be defined and implemented, and other functions in the DMP library are these interfaces called. Functions of get_ms and reg_int_cb are the timestamp and callback functions, respectively. ‘labs’ is a function of the Linux standard library (stdlib. h); ‘fabsf’ and ‘fabs’ are available in the math library (math. h). The parameter of ‘fabsf’ is float and the parameter of ‘fabs’ is a double float, thus in this program, ‘fabsf’ is used with the float parameters.

image-20211218220614253

Then functions of i2cwrite, i2cRead, and delay_ms are implemented by the following codes.

image-20211218220627697

The parameter list of the read-write function and function prototype is determined. Therefore, the function prototype could not be defined casually to avoid error prompting during compilation. The parameter list consists of the slave address, register, data length, and data content. The address uses 7-bit address mode. I2c and SMBus are used in this code. I2C is a two-wire communication protocol developed by Philips, which is often used in small devices that don’t require high speed. SMBus is a system management bus based on the I2C protocol. I2c and SMBus are compatible in the system generally. Firstly, the ioctl function is used to set the slave address, and then call the i2c read and write functions to read and write data. The delay millisecond function is implemented using the usleep function which is a subtle function and multiplied by 1000 to achieve millisecond delay.

After completing the i2c read-write function and delay function, the data of mpu6050 will be read next. The first thing to realize is to read the ID of mpu6050 to ensure normal communication; then configure mpu6050, set clock, acceleration, and gyroscope range, etc. Then invoke the api of the DMP library to initialize MPU and DMP and read the DMPU data to calculate the angle to control the speed of the car in code below

Then the roll and pitch angles will be computed by the following code. Then the roll and pitch angles will be computed to control the motor speed in the next section.

Encoder

How to get the correct speed of our cute ‘WALL-E’ robot? The position angle of the balance car only relying on the feedback of the MPU6050 sensor could not maintain a static balance. It must be combined with the speed feedback of the motor to achieve a stable self-balance. Generally, there are two methods to measure motor speed, one is the Hall encoder, and the other is the photoelectric encoder. The accuracy of the photoelectric encoder is higher than the Hall encoder with a higher price. Therefore, in our project, we use the Hall encoder for our WALL-E robot (Fig. 8).

image-20211218231451014

There are two motors to control the speed and each motor has a hall encoder then are connected to RPI gpio12, gpio16, gpio20, and gpio21.

image-20211218231558423

The encoder has two phases: A phase and B phase. They are exactly 90 degrees out of phase in the output of the waveform and this phase difference is used to judge the motor's forward and reverse rotation. As shown in picture below.

image-20211218231606766

There are two-phase waveform diagrams of the motor forward and reverse rotation. In the case of motor forward rotation, when phase A runs ahead of phase B, B signal is always low at the rising edge of A, A is always high at the rising edge of B, B is high at the falling edge of A, and A is low at the falling edge of B. In the case of motor reverse rotation, A signal is always low at the rising edge of B, B is always high at the rising edge of A, A is high at the falling edge of B, and B is low at the falling edge of A.

Besides, four-time frequency speed measurement is implemented by cooperation and computation of A, B phase signals. That is, several times are counted at each rising and falling edge so that there can be 4 counts in one cycle to improve the accuracy and rate of speed measurement. To improve the timeliness of response, GPIO interrupts are used to capture the signals of the encoders. The interrupt of the Linux system is managed by the kernel and could not be used at the application layer. Therefore, it is necessary to implement the program in the kernel. This is equivalent to the driver of the encoder, and it is quite suitable with the kernel module. As shown this picture,

image-20211218231945640

In the init function, mode requirement and configuration are set to use GPIO. Function GPIO_requirement () with three parameters of GPIO number, mode, and the name. Function enable_RQ is used to enable the interrupt and its parameter is the interrupt number. The interrupt number corresponding to GPIO is obtained by function GPIO_interrput (). As shown in pciture

image-20211218232232950

the function REQ_interrupt is used to configure the interrupt callback function, trigger mode, name, and input parameters. The interrupt function of Linux is divided into two parts: the top half and the bottom half. The top half is the real callback function. The execution is in the interrupt context and generally could not be interrupted to execute the part that must be done. The bottom half is triggered by the top half, and other less urgent matters that interrupt callbacks are executed. The programs in the bottom half will participate in the kernel scheduling and could be interrupted.

In our RPI, 4-time frequency speed measurement is used and if the rotation is forward, the count of the encoder will be increased by 1, and if the rotation is reversed, the count of the encoder will be decreased by 1, as shown in pciture

image-20211218232634709

Because the count of the encoder is proportional to the motor speed, it is no need to compute the specific speed of the motor. Therefore, the count values are directly used as the speed feedback of the PID controller as the following code.

image-20211218232544884

Performance of Program

As the result shown below, this program reads 57 times/second of angel. Then set PWM signal to TB6612 29 times/second. It is really fast than python code so we successfully to let the Pi WALL-E self balanced.

RESULT

As shown in the video, we design a two wheel self-balanced robot and add an cute eye on the top of the robot. Then name this cute robot as Pi WALL-E. We set an Android app connected to the phone to control the gimbal and rotate the camera, thus we can see the world in different directions from WALL-E’s eye.

Pi WALL-E only has two wheels but it can self balanced very well! We can also see the video on the phone from the Pi WALL-E's eye and control the eye move around.


Work Distribution

Generic placeholder image

Joe, Us, and Pi WALL-E!

Generic placeholder image

Yucheng Peng

yp284@cornell.edu

Design the Balancing, Android, and RPi CODEs.
Complete final html file.

Generic placeholder image

Yuchen Lu

yl2237@cornell.edu

Assemble the Pi WALL-E and debug the CODEs.
Write and complete final report.

 


Parts List

  • Raspberry Pi $35.00
  • Raspberry Pi Camera V2 $25.00
  • MPU6050 $3
  • TB6612 $0
  • Gimbal $0
  • 12V Motors with encoder $13
  • Structure $60

Total: $101


References

PiCamera Document
Pigpio Library
R-Pi GPIO Document
Pi Robot

Code Appendix

We put all our CODEs in the github! See the linke below.

https://github.com/Typo90/ECE5725-Self-Balanced-Robot


Thanks

Here, we sincerely appreciate all of you that provides help in the success of Pi WALL-E.
First and foremost, thank Professor Joe Skovire. You provdie a platform for us to bulid this cute Pi WALL-E robot. And we also really appreciate your suggestions like version control and how to make python code faster.
Thank all TAs that help us to dubug and to find some components from lab.
Thank Guirong Lin that shipped the 12V motors and whole structure of the Pi WALL-E from China
Thank Yuchong Geng's 32g SDcard. And thank you for spending a whole night debugging with us.
Thank Hehong Li's android phone that helped us get through the hard time during the loss of our phone.
Thank Haowen Jiang and Ruidi Peng's Raspberry Pi and Pi camera.
Thank Jiao Yang's motors and TB6612.
Thank Kuan Lu's access of maker lab and encouragement for us.
Thank Hanzhong Liang's help to PID paremters adjustment.